package com.app.couponsharelife.utils

import android.content.Context
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.WXAPIFactory

/**
 *Created by xct on 2019/1/19
 *Description:
 */
class PayUtil {
    /**
 * 微信支付
 */
     fun wechatPay(context: Context, appid:String, partnerid:String, prepayid:String,
                   noncestr:String, timestamp:String, pkg:String,
                   sign:String) {
         //        //首先在调用之前，需要先在代码中进行微信API注册
         //        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, null);
         //        // 将该app注册到微信
         //        wxApi.registerApp(appid);
            if (isWXAppInstalledAndSupported(context)) {
                val req = PayReq()
                req.appId = appid
                req.partnerId = partnerid
                req.prepayId = prepayid
                req.packageValue = pkg
                req.nonceStr = noncestr
                req.timeStamp = timestamp
                req.packageValue = pkg
                req.sign = sign
                WXAPIFactory.createWXAPI(context, appid).sendReq(req)
            } else {
                ToastUtil.getInstance().showToast("您必需要安装微信才能发起微信支付哦！")
            }

        }

       private fun isWXAppInstalledAndSupported(context:Context):Boolean {
            val msgApi = WXAPIFactory.createWXAPI(context, null)
           return msgApi.isWXAppInstalled
        }
}